package main;

import gui.GuiClient;
import model.GameRecord;
import model.GameState;
import model.GameType;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

/**
 * @author Alex Vikharev vikharev@yandex-team.ru
 *         created 23.02.12
 */
public class GameWatcher {

    public static void watchGame(GameRecord game) {
        GuiClient client = new GuiClient();
        GameState state = GameState.SECOND_PLAYER_MOVE;
        for (int i = 0; i < game.getMovesCount(); i++) {
            int move = i / 2;
            if (i % 2 == 0) {
                if (i == game.getMovesCount() - 1) {
                    state = GameState.FIRST_PLAYER_WIN;
                }
                client.watch(game.getFirstPlayerMoves().get(move), state);
                state = GameState.FIRST_PLAYER_MOVE;
            } else {
                if (i == game.getMovesCount() - 1) {
                    state = GameState.SECOND_PLAYER_WIN;
                }
                client.watch(game.getSecondPlayerMoves().get(move), state);
                state = GameState.SECOND_PLAYER_MOVE;
            }
        }
    }

    public static void main(String[] args) throws IOException, InterruptedException {
        InputStream input = new FileInputStream("LightLookAhead(0x1)games100000");
        byte[] b = new byte[16];
        while (input.read(b, 0, 16) != -1) {
            final GameRecord gameRecord = GameRecord.fromBytes(b, GameType.SIMPLE);
            watchGame(gameRecord);
            Thread.sleep(10000);
        }

        input.close();
    }
}
